package com.newbingo.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.newbingo.dao.imple.IGameTableDao;
import com.newbingo.dto.imple.IGameTableDto;
import com.newbingo.entity.BettingCell;
import com.newbingo.entity.GamePlay;
import com.newbingo.entity.GameTable;
import com.newbingo.util.Pager;

public class GameTableDto implements IGameTableDto, Serializable {

	private Integer gtno; // 游戏桌编号
	private String deskName;// 桌名
	private String website;// 网址
	private String status;// 状态
	private String openTime = "";// 开场时间
	private String closeTime = "";// 关场时间
	private String endBettingTimeout = "";// 投注截止时间
	private String drawLotteryInterval = "";// 每期间隔时间
	private PeriodGameDto currentGame;
	private Set<BettingCellDto> bettingCells = new HashSet<BettingCellDto>();

	private IGameTableDao gametableDao = null;

	/*
	 * 查询出的GameTable Entity转换为DTO对象
	 */
	public Pager findByPage(int start, int limit, int gameno) {
		Pager page = gametableDao.findByPage(start, limit, gameno);
		List<GameTable> list = page.getData();
		List<GameTableDto> gt = new ArrayList<GameTableDto>();
		Pager pager = new Pager();
		pager.setTotalSize(page.getTotalSize());
		GameTableDto ddt = null;
		if (list != null && list.size() > 0) {
			for (GameTable gameTable : list) {
				ddt = changeToDto(gameTable);
				gt.add(ddt);
			}
		}
		pager.setData(gt);
		return pager;
	}

	public GameTableDto findById(Integer id) {
		GameTable gameTable = gametableDao.findByGameTableId(id);
		if (gameTable != null) {
			return changeToDto(gameTable);
		} else {
			return null;
		}
	}

	/*
	 * GameTable Entity对象转换成GameTableDto
	 */
	public GameTableDto changeToDto(GameTable gameTable) {
		GameTableDto ddt = new GameTableDto();
		ddt.setGtno(gameTable.getGtno());
		ddt.setDeskName(gameTable.getName());
		ddt.setWebsite(gameTable.getWebsite());
		ddt.setStatus(gameTable.getStatus());
		if (gameTable.getCurrentPeriodGame() != null) {
			PeriodGameDto pgDto = new PeriodGameDto();
			ddt.setCurrentGame(pgDto.changeToDto(gameTable
					.getCurrentPeriodGame()));
		}
		if (gameTable.getOperationTime() != null) {
			if (gameTable.getOperationTime().getOpenTime() == null) {
				ddt.setOpenTime("");
			} else {
				ddt
						.setOpenTime(gameTable.getOperationTime().getOpenTime()
								+ "");
			}
			if (gameTable.getOperationTime().getClass() == null) {
				ddt.setCloseTime("");
			} else {
				ddt.setCloseTime(gameTable.getOperationTime().getCloseTime()
						+ "");
			}
			if (gameTable.getOperationTime().getEndBettingTimeout() == null) {
				ddt.setEndBettingTimeout("");
			} else {
				ddt.setEndBettingTimeout(gameTable.getOperationTime()
						.getEndBettingTimeout()
						+ "");
			}
			if (gameTable.getOperationTime().getDrawLotteryInterval() == null) {
				ddt.setDrawLotteryInterval("");
			} else {
				ddt.setDrawLotteryInterval(gameTable.getOperationTime()
						.getDrawLotteryInterval()
						+ "");
			}
		} else {
			ddt.setOpenTime("");
			ddt.setCloseTime("");
			ddt.setEndBettingTimeout("");
			ddt.setDrawLotteryInterval("");
		}
		// 设置游戏桌所属游戏的投注格
		Set<GamePlay> gamePlays = gameTable.getGame().getPlays();
		if (gamePlays != null && gamePlays.size() > 0) {
			for (GamePlay gamePlay : gamePlays) {
				Set<BettingCell> bettingCells = gamePlay.getBettingCells();
				if (bettingCells != null && bettingCells.size() > 0) {
					for (BettingCell bettingCell : bettingCells) {
						BettingCellDto bettingCellDto = new BettingCellDto();
						ddt.getBettingCells().add(
								bettingCellDto.changeToDto(bettingCell));
					}
				}
			}
		}
		return ddt;
	}

	public Integer getGtno() {
		return gtno;
	}

	public void setGtno(Integer gtno) {
		this.gtno = gtno;
	}

	public String getDeskName() {
		return deskName;
	}

	public void setDeskName(String deskName) {
		this.deskName = deskName;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getOpenTime() {
		return openTime;
	}

	public void setOpenTime(String openTime) {
		this.openTime = openTime;
	}

	public String getCloseTime() {
		return closeTime;
	}

	public void setCloseTime(String closeTime) {
		this.closeTime = closeTime;
	}

	public String getEndBettingTimeout() {
		return endBettingTimeout;
	}

	public void setEndBettingTimeout(String endBettingTimeout) {
		this.endBettingTimeout = endBettingTimeout;
	}

	public String getDrawLotteryInterval() {
		return drawLotteryInterval;
	}

	public void setDrawLotteryInterval(String drawLotteryInterval) {
		this.drawLotteryInterval = drawLotteryInterval;
	}

	public IGameTableDao getGameTableDao() {
		return gametableDao;
	}

	public void setGameTableDao(IGameTableDao gametableDao) {
		this.gametableDao = gametableDao;
	}

	public IGameTableDao getGametableDao() {
		return gametableDao;
	}

	public void setGametableDao(IGameTableDao gametableDao) {
		this.gametableDao = gametableDao;
	}

	public PeriodGameDto getCurrentGame() {
		return currentGame;
	}

	public void setCurrentGame(PeriodGameDto currentGame) {
		this.currentGame = currentGame;
	}

	public Set<BettingCellDto> getBettingCells() {
		return bettingCells;
	}

	public void setBettingCells(Set<BettingCellDto> bettingCells) {
		this.bettingCells = bettingCells;
	}
}
